package com.youxue.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 15c on 2018/1/28.
 */
//看用户是否登陆
public class AdminFilterControl implements Filter {
    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest hsq = (HttpServletRequest) req;
        String uname = (String) hsq.getSession().getAttribute("admin");
        if (uname == null) {
            ((HttpServletResponse) resp).sendRedirect(hsq.getContextPath() + "/login");
        } else {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}
